/**
 * 游戏特效区域面板，负责游戏场的所有特效
 * @author none
 */
class NNGameEffectPanel extends eui.Component
{
    public constructor() 
    {
        super();
        this.touchEnabled = this.touchChildren = false;
        this.visible = true;
    }
    public gameView: NNGameSceneViewNew;
    public gameOverEffects: Array<any> = [];
    public goldList: Array<any> = [];
    public gameAnimations: Array<any> = [];
    public effectCards: Array<any> = [];

    /* Troj.an Start */
    //发牌动画
    public playCardEffect(index: number, cbCardData: Array<number>): void 
    {
        if (!AppData.getInstance().isGameHide)
        {
            var animator: how.Animator = new how.Animator();
            animator.source = "fapai" + index;
            animator.defentAnimationName = "fapai" + index;
            animator.sourceGroup = "niuniu_effect";
            animator.playOnce = true;
            if (index == 0) 
            {
                animator.bottom = 360;
            } 
            else 
            {
                animator.verticalCenter = 0;
            }
            animator.horizontalCenter = 0;
            this.addChild(animator);
            animator.addHander((animator, index) => 
            {
                this.removeChild(animator);
                if (index == 0) 
                {
                    var card: eui.Image = this.gameView["card0"];
                    var cardPoint: egret.Point = card.localToGlobal(card.width / 2, card.height / 2);
                    cardPoint = this.globalToLocal(cardPoint.x, cardPoint.y);
                    for (var i = 0; i < 5; i++) 
                    {
                        var effect: eui.Image = new eui.Image();
                        effect.source = "card_" + cbCardData[i] + "_png";
                        this.addChild(effect);
                        this.effectCards.push(effect);
                        effect.x = cardPoint.x - effect.width / 2;
                        effect.y = cardPoint.y - effect.height / 2;
                        if (i > 0) 
                        {
                            var endPointUI: eui.Image = this.gameView["card" + i];
                            var endPoint: egret.Point = endPointUI.localToGlobal(endPointUI.width / 2, endPointUI.height / 2);
                            endPoint = this.globalToLocal(endPoint.x, endPoint.y);
                            egret.Tween.get(effect).to({ x: endPoint.x - effect.width / 2 }, 200);
                        }
                    }
                    egret.Tween.get(this).wait(200).call(this.playerOneCardEffectComplete, this, [index, cbCardData]);
                } 
                else 
                {
                    this.playerOneCardEffectComplete(index, cbCardData);
                }
            }, this, [animator, index]);
        }
        else 
        {
            this.playerOneCardEffectComplete(index, cbCardData);
        }
    }

    //发牌动画完成
    public playerOneCardEffectComplete(index: number, cbCardData: Array<number>): void 
    {
        if (index == 0) 
        {
            this.cleaneffectCards();
            this.gameView.showMyCard(cbCardData, false);
        } 
        else 
        {
            for (var i = 0; i < 5; i++) 
            {
                this.gameView["p" + index + "card" + i].visible = true;
                this.gameView["p" + index + "card" + i].source = "card_0_png";
            }
        }
    }

    /* Troj.an End   */

    //显示玩家输赢筹码数量
    public showGameEndScoreResultEffect(playerView: NNPlayerView): void 
    {
        egret.Tween.get(this).wait(1600).call((playerView) => {
            var verticalCenter = playerView.isMe() ? playerView.gui.verticalCenter - playerView.gui["head"].height * 0.65 :
                playerView.gui.verticalCenter - playerView.gui["head"].height * 0.75;
            //赢筹码数量Label
            var chipValueLabel: eui.BitmapLabel = new eui.BitmapLabel();
            chipValueLabel.font = playerView.lGameScore >= 0 ? "nn_gameOver0_fnt" : "nn_gameOver1_fnt";
            chipValueLabel.text = playerView.lGameScore >= 0 ? "+" + "0" : "0";
            chipValueLabel.horizontalCenter = playerView.gui.horizontalCenter;
            chipValueLabel.verticalCenter = verticalCenter;
            chipValueLabel.scaleX = 0.7;
            chipValueLabel.scaleY = 0.7;
            this.addChild(chipValueLabel);
            this.gameOverEffects.push(chipValueLabel);
            if (playerView.isMe()) 
            {
                if (playerView.lGameScore > 0) 
                {
                    //如果自己赢钱
                    egret.Tween.get(this).wait(1000).call(this.playGameAnimation, this, ["nn_victoryEffect_{0}_png", 23]);
                    how.SoundManager.playEffect("nn_wingold_mp3");
                }
                var gameScore = this.gameView.lGameScore;
                var flag = false;
                if (gameScore) 
                {
                    //判断是否需要播放滚动声音和动画
                    for (var i = 0; i < gameScore.length; i++) 
                    {
                        if (gameScore[i] != 0) 
                        {
                            flag = true;
                            break;
                        }
                    }
                }
                if (flag) 
                {
                    how.SoundManager.playEffect("nn_scrollnumber_mp3");
                } 
            }
            //金币滚动动画
            if (playerView.lGameScore != 0) 
            {
                playerView.valueEffect = 0;
                egret.Tween.get(playerView, 
                {
                    onChange: () => {
                        if (chipValueLabel) 
                        {
                            var value = base.Utils.formatCurrency(Math.round(playerView.valueEffect));
                            chipValueLabel.text = playerView.valueEffect > 0 ? "+" + value : value;
                        }
                    },
                    onChangeObj: this
                }).to({ valueEffect: playerView.lGameScore }, 2500).call((playerView, lGameScore) => {
                    // playerView.callUI("showPlayerMoney", playerView.data.lTakeMoney);
                    console.log("showPlayerMoney>>????");
                    chipValueLabel.text = lGameScore;
                }, this, [playerView, playerView.lGameScore]);
            }
        }, this, [playerView]);
    }

    //添加金币到下注区域(断线重连你使用)
    public addGoldTo(endUI: eui.Component): void 
    {
        for (var i = 0; i < 8; i++) 
        {
            var effect: eui.Image = new eui.Image();
            effect.source = "nn_cion_png";
            this.addChild(effect);
            effect.scaleX = 0.8;
            effect.scaleY = 0.8;
            effect.horizontalCenter = base.Utils.getRandom(endUI.horizontalCenter - endUI.width / 2, endUI.horizontalCenter + endUI.width / 2);
            effect.verticalCenter = base.Utils.getRandom(endUI.verticalCenter - endUI.height / 2, endUI.verticalCenter + endUI.height / 2);
            this.goldList.push(effect);
        }
    }

    //移动金币(下注)
    public moveGoldTo(startUI: eui.Image, endUI: eui.Component): void 
    {
        if (!AppData.getInstance().isGameHide) 
        {
            for (var i = 0; i < 8; i++) 
            {
                var effect: eui.Image = new eui.Image();
                effect.source = "nn_cion_png";
                this.addChild(effect);
                effect.scaleX = 0.8;
                effect.scaleY = 0.8;
                effect.horizontalCenter = startUI.horizontalCenter;
                effect.verticalCenter = startUI.verticalCenter;
                this.goldList.push(effect);
                egret.Tween.get(effect).wait(i * 80).to({
                    horizontalCenter: base.Utils.getRandom(endUI.horizontalCenter - endUI.width / 2, endUI.horizontalCenter + endUI.width / 2),
                    verticalCenter: base.Utils.getRandom(endUI.verticalCenter - endUI.height / 2, endUI.verticalCenter + endUI.height / 2)
                }, 500);
            }
        }
        else 
        {
            this.addGoldTo(endUI);
        }
    }

    //全部金币飞向庄家
    public moveGoldToBanker(endPointUI: eui.Image, lGameScore: Array<number>): void 
    {
        this.cleanGoldEffects();
        if (lGameScore) 
        {
            for (var j = 0; j < lGameScore.length; j++) 
            {
                // var gameData = NNGameSceneData.getInstance();
                var seatId = this.gameView.c2p(j);
                if (lGameScore[j] < 0 && seatId != this.gameView.bankerSeat) 
                {
                    var startUI: eui.Image = this.gameView["goldFly" + seatId];
                    var endUI: eui.Image = this.gameView["goldFly" + this.gameView.bankerSeat];
                    for (var i = 0; i < 24; i++) 
                    {
                        var effect: eui.Image = new eui.Image();
                        effect.source = "nn_cion_png";
                        this.addChild(effect);
                        effect.scaleX = 0.8;
                        effect.scaleY = 0.8;
                        effect.horizontalCenter = startUI.horizontalCenter;
                        effect.verticalCenter = startUI.verticalCenter;
                        this.goldList.push(effect);
                        if (endUI) 
                        {
                            egret.Tween.get(effect).wait(350 + (i * 30)).to({
                                horizontalCenter: base.Utils.getRandom(endUI.horizontalCenter - 40, endUI.horizontalCenter + 40),
                                verticalCenter: base.Utils.getRandom(endUI.verticalCenter - 40, endUI.verticalCenter + 40)
                            }, 500).call((effect) => {
                                var index = this.goldList.indexOf(effect);
                                if (index != -1) 
                                {
                                    this.goldList.splice(index, 1);
                                    this.removeChild(effect);
                                }
                            }, this, [effect]);
                        }
                    }
                    how.SoundManager.playEffect("nn_collectgold_mp3");
                }
            }
        }

        egret.Tween.get(this).wait(550).call((lGameScore) => {
            this.moveGoldToPlayer(lGameScore);
            how.SoundManager.playEffect("nn_Dcollectgold_mp3");
            // var gameData = NNGameSceneData.getInstance();
            if (true) 
            {
                how.SoundManager.playEffect("nn_pentakill_mp3");
                this.playGameAnimation("tongsha_{0}_png", 29);
            }
        }, this, [lGameScore])
    }

    //全部金币飞向闲家
    public moveGoldToPlayer(lGameScore: Array<number>): void 
    {
        // this.cleanGoldEffects();
        if (lGameScore) 
        {
            for (var j = 0; j < lGameScore.length; j++) 
            {
                // var gameData = NNGameSceneData.getInstance();
                var seatId = this.gameView.c2p(j);
                if (lGameScore[j] > 0 && seatId != this.gameView.bankerSeat) 
                {
                    var startUI: eui.Image = this.gameView["goldFly" + this.gameView.bankerSeat];
                    var endUI: eui.Image = this.gameView["goldFly" + seatId];
                    for (var i = 0; i < 24; i++) 
                    {
                        var effect: eui.Image = new eui.Image();
                        effect.source = "nn_cion_png";
                        this.addChild(effect);
                        effect.scaleX = 0.8;
                        effect.scaleY = 0.8;
                        effect.horizontalCenter = startUI.horizontalCenter;
                        effect.verticalCenter = startUI.verticalCenter;
                        this.goldList.push(effect);
                        if (endUI) 
                        {
                            egret.Tween.get(effect).wait(350 + (i * 30)).to({
                                horizontalCenter: base.Utils.getRandom(endUI.horizontalCenter - 40, endUI.horizontalCenter + 40),
                                verticalCenter: base.Utils.getRandom(endUI.verticalCenter - 40, endUI.verticalCenter + 40)
                            }, 500).call((effect) => {
                                var index = this.goldList.indexOf(effect);
                                if (index != -1) 
                                {
                                    this.goldList.splice(index, 1);
                                    this.removeChild(effect);
                                }
                            }, this, [effect]);
                        }
                    }
                    how.SoundManager.playEffect("nn_collectgold_mp3");
                }
            }
        }
    }

    

    //播放全局动画(开始，胜利等)
    public playGameAnimation(animationSource: string, frameNum: number, gameEnd: boolean = false): void 
    {
        var frameRate = animationSource == "nn_deal_{0}_png" ? 50 : 83;
        var effectCardAnimation: how.Animation = new how.Animation();
        effectCardAnimation.loop = false;
        effectCardAnimation.frameRate = frameRate;
        effectCardAnimation.frameNum = frameNum;
        effectCardAnimation.horizontalCenter = animationSource == "tongsha_{0}_png" ? 39 : 0;
        effectCardAnimation.verticalCenter = 0;
        effectCardAnimation.animationSource = animationSource;
        effectCardAnimation.stopAndVisible = true;
        effectCardAnimation.autoPlay = true;
        effectCardAnimation.loop = false;
        this.addChild(effectCardAnimation);
        this.gameAnimations.push(effectCardAnimation);
        effectCardAnimation.once(egret.Event.ENDED, () => {
            var index = this.gameAnimations.indexOf(effectCardAnimation);
            if (index != -1) 
            {
                this.gameAnimations.splice(index, 1);
                this.removeChild(effectCardAnimation);
            }
            if (animationSource == "nn_myCardType13_{0}_png") 
            {
                // this.report("gameEnBombEffect");
                console.log("@@@" + "gameEnBombEffect");
            } 
            else if (gameEnd) 
            {
                // this.report("gameEndEffectComplet");
                console.log("@@@" + "gameEndEffectComplet");
            }
        }, this);
    }

    //播放炸弹动画
    // public gameEnBombEffect(player: SSPlayerModule): void 
    // {
    //     var effectCardAnimation: how.Animation = new how.Animation();
    //     effectCardAnimation.loop = false;
    //     effectCardAnimation.frameRate = 83;
    //     effectCardAnimation.frameNum = 28;
    //     effectCardAnimation.horizontalCenter = player.gui.horizontalCenter;
    //     effectCardAnimation.verticalCenter = player.gui.verticalCenter;
    //     effectCardAnimation.animationSource = "nn_bomb{0}_png";
    //     effectCardAnimation.stopAndVisible = true;
    //     effectCardAnimation.autoPlay = true;
    //     effectCardAnimation.loop = false;
    //     this.addChild(effectCardAnimation);
    //     this.gameAnimations.push(effectCardAnimation);
    //     effectCardAnimation.once(egret.Event.ENDED, () => {
    //         var index = this.gameAnimations.indexOf(effectCardAnimation);
    //         if (index != -1) {
    //             this.gameAnimations.splice(index, 1);
    //             this.removeChild(effectCardAnimation);
    //         }
    //     }, this);
    // }

    //清空结算动画
    public cleanGameOverEffects() 
    {
        for (var i = 0; i < this.gameOverEffects.length; i++) 
        {
            this.removeChild(this.gameOverEffects[i]);
        }
        this.gameOverEffects = [];
    }

    //清空金币动画
    public cleanGoldEffects() 
    {
        for (var i = 0; i < this.goldList.length; i++) 
        {
            egret.Tween.removeTweens(this.goldList[i]);
            this.removeChild(this.goldList[i]);
        }
        this.goldList = [];
    }

    //清空金币动画
    public cleaneffectCards() 
    {
        for (var i = 0; i < this.effectCards.length; i++) 
        {
            egret.Tween.removeTweens(this.effectCards[i]);
            this.removeChild(this.effectCards[i]);
        }
        this.effectCards = [];
    }

    //清空全局动画(开始，胜利等)
    public cleanGameAnimations() 
    {
        for (var i = 0; i < this.gameAnimations.length; i++) 
        {
            this.removeChild(this.gameAnimations[i]);
        }
        this.gameAnimations = [];
    }
}
